/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BARenewLoansViewController.m
//  redia-bapps-ios
//
//  Created by Henrik Lynbech on 29/08/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BARenewLoansViewController.h"
#import "BALoan.h"
#import "BAStandardResultObject.h"
#import "BAPatronLoansViewController.h"
#import "BALoginViewController.h"

@interface BARenewLoansViewController ()

@end

@implementation BARenewLoansViewController


+(void)createRenewAllLoansDialogFromViewController:(BAPatronLoansViewController*)parentController
{
    BARenewLoansViewController* new_dialog_viewcontroller = [[self class] createBlackCoverDialog];
    new_dialog_viewcontroller->parentLoansViewController = parentController;
    [new_dialog_viewcontroller performRenewAllLoans];
    
}

-(void)performRenewAllLoans
{
    [self.loadingIndicator startAnimating];
    
    [BALoginViewController checkLoginStatusFromViewController:self
                                                  resultBlock:^{
                                                      
                                                      
                                                      [BARPCManager renewAllLoansAsyncFromView:self
                                                                                   resultBlock:^(NSDictionary *result) {
                                                                                       [self.loadingIndicator stopAnimating];
                                                                                       int totalcount = [result count];
                                                                                       int successcount = 0;
                                                                                       for (NSString* identifier in [result allKeys]) {
                                                                                           BAStandardResultObject* std_result_item = [result objectForKey:identifier];
                                                                                           DLog(@"result %@: %d", identifier,  std_result_item.result);
                                                                                           if (std_result_item.result) {
                                                                                               successcount++;
                                                                                           }
                                                                                       }
                                                                                       
                                                                                       NSString* msg;
                                                                                       if (successcount==0) {
                                                                                           msg = @"Ingen af dine lån kunne fornyes";
                                                                                       } else if (successcount==totalcount) {
                                                                                           msg = @"Alle dine lån kunne fornyes";
                                                                                       } else {
                                                                                           msg = [NSString stringWithFormat:@"%d ud af %d lån blev fornyet", successcount, totalcount];
                                                                                       }
                                                                                       
                                                                                       UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Forny lån" message:msg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                                                                                       [alert show];
                                                                                   }
                                                       
                                                                                    errorBlock:^(int errorCode, NSString *errorMessage) {
                                                                                        [self.loadingIndicator stopAnimating];
                                                                                        UIAlertView* alerterror = [[UIAlertView alloc] initWithTitle:@"Netværksfejl"
                                                                                                                                             message:[NSString stringWithFormat:@"Kunne ikke forny alle lån. %@",errorMessage]
                                                                                                                                            delegate:self
                                                                                                                                   cancelButtonTitle:@"OK"
                                                                                                                                   otherButtonTitles:nil];
                                                                                        [alerterror show];
                                                                                    }];
                                                  } errorBlock:^(int errorCode, NSString *errorMessage) {
                                                      // Authentication error.
                                                      
                                                      [parentLoansViewController initiateRefreshLoans];
                                                      [self dismissBlackCover];
                                                      
                                                      //don't show alert here, it is already presented by BALoginViewController
                                                  }];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [parentLoansViewController initiateRefreshLoans];
    [self dismissBlackCover];
}

@end
